cmake_minimum_required(VERSION 3.12)
project(TritonKernelLoader VERSION 1.0.0 LANGUAGES CXX)
# Top of CMakeLists.txt
add_compile_definitions(_GLIBCXX_USE_CXX11_ABI=0)
# 设置C++标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(ASCEND_HOME_PATH $ENV{ASCEND_HOME_PATH})

set(GTEST_ROOT "/home/z00881607/triton-workspace/gtest_abi0")
find_package(GTest REQUIRED PATHS ${GTEST_ROOT})

if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Release)
endif()




# 设置编译选项
set(CMAKE_CXX_FLAGS "-Wall -Wextra")
set(CMAKE_CXX_FLAGS_DEBUG "-g")
set(CMAKE_CXX_FLAGS_RELEASE "-O3")

if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
    set(PLATFORM "x86_64-linux")
elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64" OR CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64")
    set(PLATFORM "aarch64-linux")
else()
    message(WARNING "Unsupported architecture: ${CMAKE_SYSTEM_PROCESSOR}")
    # 可以设置一个默认值或直接报错
    set(PLATFORM "unknown")
endif()

set(TORCH_API_SRC
    src/torch_api/npu_triton_add_kernel.cpp
)

set(ADAPTER_SRC
    src/kernel_registry.cpp
    src/kernel_handle.cpp
    src/kernel_loader.cpp
)

# 创建静态库
add_library(triton_kernel_loader STATIC
    ${ADAPTER_SRC}
    ${TORCH_API_SRC}
)

# Set include directories for the library
# Use PUBLIC for headers that need to be accessible by consumers of this library
target_include_directories(triton_kernel_loader
    PUBLIC
        $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include>
        $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/third_party/nlohmannJson/single_include>
    PRIVATE
        $<BUILD_INTERFACE:${ASCEND_HOME_PATH}/${PLATFORM}/include>
        $<BUILD_INTERFACE:${ASCEND_HOME_PATH}/${PLATFORM}/include/experiment/platform>
        $<BUILD_INTERFACE:${ASCEND_HOME_PATH}/${PLATFORM}/include/experiment/msprof>
        ${GTEST_ROOT}/include
        /usr/local/lib/python3.11/site-packages/torch/include
        /usr/local/lib/python3.11/site-packages/torch_npu/include
        /usr/local/lib/python3.11/site-packages/torch/include/torch/csrc/api/include
)

# Link directories for the library
link_directories(
    ${ASCEND_HOME_PATH}/lib64
    /usr/local/lib/python3.11/site-packages/torch/lib
    /usr/local/lib/python3.11/site-packages/torch_npu/lib
)

target_link_libraries(triton_kernel_loader 
    runtime
    ascendcl
)

# Note: test.cpp and test_torch_add_kernel.cpp are kept for reference
# but are no longer built. All tests are now managed in the test/ subdirectory.

# Add test subdirectory (manages all gtest-based unit tests)
add_subdirectory(test)

# 安装目标
install(TARGETS triton_kernel_loader
    ARCHIVE DESTINATION lib
    LIBRARY DESTINATION lib
)

install(DIRECTORY include/
    DESTINATION include
    FILES_MATCHING PATTERN "*.h"
)


